package com.thor.palmmall.util.pay;

import javax.servlet.http.HttpServletRequest;

public class IpUtil {
  /**
   * 获取登录用户IP地址
   * 
   * @param request
   * @return
   */
  public static String getIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    if (ip.equals("0:0:0:0:0:0:0:1")) {
      ip = "127.0.0.1";
    }
    return ip;
  }

  /**
   * 取域名。
   * 
   * @param request
   * @return
   */
  public static String getDomain(HttpServletRequest request) {
    StringBuffer url = request.getRequestURL();
    String domain = url.delete(url.length() - request.getRequestURI().length(), url.length())
        .append("/").toString();
    return domain;
  }

  /**
   * 取得应用根路径。
   * 
   * @param request
   * @return
   */
  public static String getWebRoot(HttpServletRequest request) {
    return getDomain(request) + "car-web";
  }

}
